/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: LogApi.java
 * Author:   admin
 * Date:     2016年1月11日 下午3:01:22
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.app.snpaasadmin.api;

import com.suning.app.snpaasadmin.exception.BoshException;
import com.suning.app.snpaasadmin.util.HttpClientResponse;
import com.suning.app.snpaasadmin.util.HttpClientUtil;
import com.suning.app.snpaasadmin.util.HttpStatusUtil;
import com.suning.app.snpaasadmin.util.JsonUtil;

import java.io.File;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.protocol.HttpContext;

/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author admin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class LogApi {

    public static Map<String, Object> getLogTaskId(HttpContext httpContext, HttpHost httpHost, String deployName, String comName, Integer index) throws Exception {
        String urlPath = httpHost.toString() + "/deployments/" + deployName +"/jobs/" +comName +"/" + index.toString() +"/logs?type=job";
        HttpClientResponse response = HttpClientUtil.doGet(urlPath, null, null, httpHost, httpContext);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new BoshException(String.valueOf(response.getStatus()), data.get("description").toString());
        }
        String content = response.getBody();
        return JsonUtil.convertJsonToMap(content);
    }
    
    public static Map<String, Object> getLogResourceId(HttpContext httpContext, HttpHost httpHost,Integer taskId) throws Exception {
        String urlPath = httpHost.toString() + "/tasks/" + taskId;
        HttpClientResponse response = HttpClientUtil.doGet(urlPath, null, null, httpHost, httpContext);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new BoshException(String.valueOf(response.getStatus()), data.get("description").toString());
        }
        String content = response.getBody();
        return JsonUtil.convertJsonToMap(content);
    }
    
    public static File downloadLog(HttpContext httpContext, HttpHost httpHost,String resultId,String deployName) throws Exception {
        String urlPath = httpHost.toString() + "/resources/" + resultId;
        File file = HttpClientUtil.doGetDownload(urlPath, null, null, httpHost, httpContext,deployName);
        /*String output="";
        if (!HttpStatusUtil.isSuccessStatus(response.getStatusLine().getStatusCode())) {
            HttpEntity resentity = response.getEntity();
            String content = null;
            if (resentity != null) {
                content = EntityUtils.toString(resentity, "UTF-8");
            }
            Map<String, Object> res = JsonUtil.convertJsonToMap(content);
            throw new BoshException(String.valueOf(response.getStatusLine().getStatusCode()),
                    res.get("description").toString());
        }
        HttpEntity resentity = response.getEntity();
        if (resentity != null) {
            output = EntityUtils.toString(resentity, "UTF-8");
        }*/
        return file;
    }
    
}
